﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using Warehouse.Web.AppCode;

namespace Warehouse.Web.Inventory
{
    public partial class Move : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataAccess.Warehouse[] houses = DataAccess.Warehouse.GetWarehouses();

                this.ddlFromWareHouse.DataSource = houses;
                this.ddlFromWareHouse.DataBind();

                this.ddlToWareHouse.DataSource = houses;
                this.ddlToWareHouse.DataBind();

                this.rptDetails.DataSource = Enumerable.Range(1, 10);
                this.rptDetails.DataBind();

                JavaScriptSerializer ser = new JavaScriptSerializer();
                base.RegisterClientScriptContent("allproducts", ser.Serialize(DataAccess.Product.GetProducts()));
                base.RegisterClientScriptContent("allproductspecifications", ser.Serialize(DataAccess.ProductSpecification.GetProductSpecifications()));
                base.RegisterClientScriptContent("allwarehouses", ser.Serialize(DataAccess.Warehouse.GetWarehouses()));
                base.RegisterClientScriptContent("allsuppliers", ser.Serialize(DataAccess.Supplier.GetSuppliers()));
            }
        }
        protected override void ProcessAjaxRequest(HttpContext context, string action)
        {
            switch (action)
            {
                case "SaveStock":
                    {
                        JavaScriptSerializer ser = new JavaScriptSerializer();
                        var stock = ser.Deserialize<DataAccess.MovingStock>(context.Request.Form["Stock"]);
                        var details = ser.Deserialize<DataAccess.MovingDetail[]>(context.Request.Form["Details"]);
                        if (stock == null || details == null || details.Length == 0)
                        {
                            WebUtil.WriteJsonError(context, "表单错误");
                        }
                        else
                        {
                            stock.SerialNumber = stock.SerialNumber.Trim();

                            if (string.IsNullOrEmpty(stock.SerialNumber))
                            {
                                WebUtil.WriteJsonError(context, "入库单编号不能为空");
                                return;
                            }
                            foreach (var d in details)
                            {
                                if (d.Count < 0)
                                {
                                    WebUtil.WriteJsonError(context, "产品数量不能为负数");
                                    return;
                                }
                            }

                            try
                            {
                                using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
                                {
                                    stock.ID = Guid.NewGuid();
                                    stock.CreateDate = DateTime.Now;
                                    stock.Creator = context.User.Identity.Name;
                                    stock.Save();
                                    foreach (var d in details)
                                    {
                                        d.StockID = stock.ID;
                                        d.Save();
                                    }
                                    scope.Complete();
                                }
                                WebUtil.WriteJsonResult(context, null);
                            }
                            catch (Exception ex)
                            {
                                WebUtil.WriteJsonError(context, ex.Message);
                            }
                        }
                    }
                    break;
                default:
                    base.ProcessAjaxRequest(context, action);
                    break;
            }
        }
    }
}